#include<iostream>
#include "Comm.hpp"

class Init
{
public:
    Init()
    {
        umask(0);
        int fd = mkfifo(gpipeFile.c_str(),gmode);
        if(fd < 0)
        {
            std::cout << "make fifo error "<<std::endl;
            return;
        }
        std::cout << "make fifo success " << std::endl;
    }

    ~Init()
    {
        int fd = unlink(gpipeFile.c_str());
        if(fd < 0)
        {
            std::cout << "unlink fifo error"<<std::endl;
            return;
        }
        std::cout << "unlink fifo success "<<std::endl;
    }


};

Init init;

class Server
{
public:
    Server():_fd(gdefultfd)
    {}

    bool OpenPipeForRead()
    {
        _fd = OpenPipe(gForRead);
        if(_fd < 0) return false;
        return true;
    }

    //输出型参数
    int RecvPipe(std::string *out)
    {
        char buf[1024];
        ssize_t n = ::read(_fd,buf,sizeof(buf)-1);
        if(n > 0)
        {
            buf[n] = 0;
            *out = buf;
        }
        return n;
    }

    void ClosePipe()
    {
        ClosePipeHelper(_fd);
    }

    ~Server()
    {
        
    }
private:
    int _fd;
};